from fastapi import APIRouter, Request, HTTPException, Header

from backend.const import *
from backend.error import error_pydantic_msg
from backend.exception import DuplicateError, ValueMissError
from backend.handler import HandleNodeSelectInfo, HandleNodeAdd, HandleNodeRefresh
from backend.model import *
from common.response import *


router = APIRouter()


@router.get("/selectinfo")
def node_selectinfo():
    try:
        return OkResponse(data=HandleNodeSelectInfo())
    except ValidationError as e:
        return ServerErrorResponse(data=error_pydantic_msg(e))
    except Exception as e:
        return ServerErrorResponse(data=repr(e))
    

@router.post("/add")
def node_add(post_body: Node):
    print(type(post_body), post_body)
    try:
        return OkResponse(data=HandleNodeAdd(post_body))
    except DuplicateError as e:
        return OkDuplicateAddResponse()
    except ValidationError as e:
        return ServerErrorResponse(data=error_pydantic_msg(e))
    except Exception as e:
        return ServerErrorResponse(data=repr(e))
    

@router.put("/refresh")
def node_refresh(post_body: PostBodyNodeRefresh):
    print(type(post_body), post_body)
    try:
        return OkResponse(data=HandleNodeRefresh(post_body))
    except ValueMissError as e:
        return OkMissDomainUrlResponse(data=repr(e))
    except ValidationError as e:
        return ServerErrorResponse(data=error_pydantic_msg(e))
    except Exception as e:
        return ServerErrorResponse(data=repr(e))